/*
Copyright (c) 2009-2010, Warwick Warp Limited
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Warwick Warp Limited nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL WARWICK WARP LIMITED BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef WARRAY_WARRAY_OPS_H_INCLUDED
#define WARRAY_WARRAY_OPS_H_INCLUDED

#include <functional>
#include <algorithm>
#include <complex>
#include "warray_nd.h"
#include "warray_help.h"
#include "warray_vmath.h"
#include "warray_blas.h"

namespace warp {

//! \brief Element-wise addition of arrays.
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr>
void add(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,r,o);
	vm::add(l.size(),l.c_array(),r.c_array(),o.c_array());
}

template<typename T,detail::warray_types::size_type N,typename Lptr>
void add_const(const const_arraynd<T,N,Lptr>& l, const T& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,o);
	vm::add_const(l.size(),l.c_array(),r,o.c_array());
}

//! \brief Element-wise subtraction of arrays.
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr> 
void sub(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,r,o);
	vm::sub(l.size(),l.c_array(),r.c_array(),o.c_array());
}

template<typename T,detail::warray_types::size_type N,typename Lptr>
void sub_const(const const_arraynd<T,N,Lptr>& l, const T& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,o);
	vm::sub_const(l.size(),l.c_array(),r,o.c_array());
}

//! \brief Element-wise negation of arrays
template<typename T,detail::warray_types::size_type N,typename Lptr>
void neg(const const_arraynd<T,N,Lptr>& l, const arraynd<T,N>& o)
{
	detail::bounds_check(l,o);
	vm::neg(l.size(),l.c_array(),o.c_array());
}

//! \brief Element-wise multiplication of arrays.
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr> 
void mul(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,r,o);
	vm::mul(l.size(),l.c_array(),r.c_array(),o.c_array());
}

template<typename T,detail::warray_types::size_type N,typename Lptr>
void mul_const(const const_arraynd<T,N,Lptr>& l, const T& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,o);
	vm::mul_const(l.size(),l.c_array(),r,o.c_array());
}

template<typename T,detail::warray_types::size_type N,typename Lptr>
void mul_const(const const_arraynd<std::complex<T>,N,Lptr>& l, const T& r, const arraynd<std::complex<T>,N>& o)
{
	detail::bounds_check(l,o);
	if(l.c_array() != o.c_array())
		blas::copy(l.size(),l.c_array(),l.stride(N-1),o.c_array(),o.stride(N-1));
	blas::scal(l.size(),r,o.c_array(),o.stride(N-1));
}

/*! 
	\brief Element-wise multiplication of complex arrays
	
	o[i] = l[i]*std::conj(r[i])
*/
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr> 
void mulconj(const const_arraynd< std::complex<T>, N, Lptr >& l, const const_arraynd< std::complex<T>, N, Rptr >& r, const arraynd< std::complex<T>, N >& o)
{
	detail::bounds_check(l,r,o);
	vm::mulconj(l.size(),l.c_array(),r.c_array(),o.c_array());
}

/*! 
	\brief Element-wise multiplication of complex arrays
	
	o[i] = l[i]*std::conj(r[i])
*/
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr> 
arraynd< std::complex<T>, N > mulconj(const const_arraynd< std::complex<T>, N, Lptr >& l, const const_arraynd< std::complex<T>, N, Rptr >& r)
{
	arraynd< std::complex<T>, N > result(l.dims());
	mulconj(l,r,result);
	return result;
}

//! \brief Element-wise division of arrays.
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr> 
void div(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,r,o);
	vm::div(l.size(),l.c_array(),r.c_array(),o.c_array());
}

template<typename T,detail::warray_types::size_type N,typename Lptr>
void div_const(const const_arraynd<T,N,Lptr>& l, const T& r, const arraynd<T,N>& o)
{
	detail::bounds_check(l,o);
	vm::div_const(l.size(),l.c_array(),r,o.c_array());
}


//! \brief Element-wise addition of arrays.
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr>
arraynd<T,N> operator+(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r)
{
	arraynd< T, N > result(l.dims());
	add(l,r,result);
	return result;
}

//! \brief Element-wise subtraction of arrays.
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr>
arraynd<T,N> operator-(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r)
{
	arraynd< T, N > result(l.dims());
	sub(l,r,result);
	return result;
}

//! \brief Element-wise negation of arrays
template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<T,N> operator-(const const_arraynd<T,N,Lptr>& l)
{
	arraynd< T, N > result(l.dims());
	neg(l,result);
	return result;
}

//! \brief Element-wise multiplication of arrays.
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr>
arraynd<T,N> operator*(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r)
{
	arraynd< T, N > result(l.dims());
	mul(l,r,result);
	return result;
}

template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<T,N> operator*(const const_arraynd<T,N,Lptr>& l, const T& r)
{
	arraynd< T, N > result(l.dims());
	mul_const(l,r,result);
	return result;
}

template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<std::complex<T>,N> operator*(const const_arraynd<std::complex<T>,N,Lptr>& l, const T& r)
{
	arraynd< std::complex<T>, N > result(l.dims());
	mul_const(l,r,result);
	return result;
}

//! \brief Element-wise division of arrays.
template<typename T,detail::warray_types::size_type N,typename Lptr,typename Rptr>
arraynd<T,N> operator/(const const_arraynd<T,N,Lptr>& l, const const_arraynd<T,N,Rptr>& r)
{
	arraynd< T, N > result(l.dims());
	div(l,r,result);
	return result;
}

template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<T,N> operator/(const const_arraynd<T,N,Lptr>& l, const T& r)
{
	arraynd< T, N > result(l.dims());
	div_const(l,r,result);
	return result;
}

template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<std::complex<T>,N> operator/(const const_arraynd<std::complex<T>,N,Lptr>& l, const T& r)
{
	arraynd< std::complex<T>, N > result(l.dims());
	mul_const(l,1/r,result);
	return result;
}

//! \brief In-place element-wise addition of arrays.
template<typename T,detail::warray_types::size_type N,typename Rptr>
arraynd<T,N>& operator+=(arraynd<T,N>& l, const const_arraynd<T,N,Rptr>& r)
{
	add(l,r,l);
	return l;
}

//! \brief In-place element-wise subtraction of arrays.
template<typename T,detail::warray_types::size_type N,typename Rptr>
arraynd<T,N>& operator-=(arraynd<T,N>& l, const const_arraynd<T,N,Rptr>& r)
{
	sub(l,r,l);
	return l;
}

//! \brief In-place element-wise multiplication of arrays.
template<typename T,detail::warray_types::size_type N,typename Rptr>
arraynd<T,N>& operator*=(arraynd<T,N>& l, const const_arraynd<T,N,Rptr>& r)
{
	mul(l,r,l);
	return l;
}

template<typename T,detail::warray_types::size_type N>
arraynd<T,N>& operator*=(arraynd<T,N>& l, const T& r)
{
	mul_const(l,r,l);
	return l;
}

template<typename T,detail::warray_types::size_type N>
arraynd<std::complex<T>,N>& operator*=(arraynd<std::complex<T>,N>& l, const T& r)
{
	mul_const(l,r,l);
	return l;
}

//! \brief In-place element-wise division of arrays.
template<typename T,detail::warray_types::size_type N,typename Rptr>
arraynd<T,N>& operator/=(arraynd<T,N>& l, const const_arraynd<T,N,Rptr>& r)
{
	div(l,r,l);
	return l;
}

template<typename T,detail::warray_types::size_type N>
arraynd<T,N>& operator/=(arraynd<T,N>& l, const T& r)
{
	div_const(l,r,l);
	return l;
}

template<typename T,detail::warray_types::size_type N>
arraynd<std::complex<T>,N>& operator/=(arraynd<std::complex<T>,N>& l, const T& r)
{
	mul_const(l,T(1)/r,l);
	return l;
}

//! \brief Conjugation of complex arrays
template<typename T,detail::warray_types::size_type N,typename Lptr>
void conj(const const_arraynd<T,N,Lptr>& l, const arraynd<T,N>& r)
{
	detail::bounds_check(l,r);
	vm::conj(l.size(),l.c_array(),r.c_array());
}

//! \brief Conjugation of complex arrays
template<typename T,detail::warray_types::size_type N,typename Lptr>
arraynd<T,N> conj(const const_arraynd<T,N,Lptr>& l)
{
	arraynd<T,N> r(l.dims());
	warp::conj(l,r);
	return r;
}


} // namespace 
#endif
